package com.marvels.approve.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.manager.dao.IOpinionDao;
import com.marvels.approve.manager.dto.OpinionDTO;
import com.marvels.approve.manager.dto.OpinionTypeDTO;
import com.marvels.approve.manager.service.IOpinionService;
import com.marvels.approve.manager.service.IOpinionTypeService;
import com.marvels.approve.process.dto.LoanApplyCaseDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.StringUtils;
@Service
public class OpinionServiceImpl implements IOpinionService {
	@Autowired
	private IOpinionDao opinionDao ;
	@Autowired
	private IOpinionTypeService opinionTypeService;
	@Override
	public OpinionDTO saveOpinion(OpinionDTO opinion) throws MarvelsBusinessException {
		if(opinion == null) {
			throw new MarvelsBusinessException("意见不能为空");
		}
		if(opinion.getId() != null) {
			throw new MarvelsBusinessException("意见不能存在ID:"+opinion.getId());
		}
		//判断存在则更新，不存在则新增
		Integer count = opinionDao.findOpinion(opinion);
		if(count>0){
			opinionDao.updateOpinion(opinion);
		}else{
			opinionDao.insertOpinion(opinion);
		}
		return opinion;
	}
	
	@Override
	public List<OpinionDTO> findOpinions(OpinionDTO opinion) throws MarvelsBusinessException {
		List<OpinionDTO> opinions = opinionDao.selectOpinion(opinion);
		return opinions;
	}
	
	@Override
	public void saveOpinions(LoanApplyCaseDTO loanApplyCase , McamLoanApplyInfoDTO loanApplyInfo,String caseResult,
			List<OpinionTypeDTO> processOpinions,String violationOpinion,List<OpinionTypeDTO> returnAppOpinions,List<OpinionTypeDTO> checkOpinionTypes ,String opinionContent , String violationOptionContent ,String returnAppOpinionContent)throws MarvelsBusinessException {
		OpinionDTO params = new OpinionDTO();
		params.setLoanId(loanApplyCase.getLoanId());
		params.setCaseVersion(loanApplyCase.getCaseVersion());
		params.setNodeCode(loanApplyCase.getCurNodeCode());
		params.setUpdatedBy(params.obtainLoginUserName());
		opinionDao.delOpinionsByCaseVersion(params);
		//简版新核心的保存审批
		initOpinion(loanApplyCase, null, opinionContent,caseResult);
		//保存复核意见
//		List<OpinionTypeDTO> processOpinions = loanChecking.getProcessOpinions();
		/*if (processOpinions != null && processOpinions.size() > 0) {
			for (OpinionTypeDTO processOpinion : processOpinions) {
				initOpinion(loanApplyCase, processOpinion.getOpinionTypeCode(), opinionContent,caseResult);
			}
		}*/
		// 违例
//		String violationOpinion = loanChecking.getViolationOpinion();
		/*if (!StringUtils.isBlank(violationOpinion)) {
			initOpinion(loanApplyCase, violationOpinion, violationOptionContent,caseResult);
		}*/
		// 退单到前端APP类型
//		List<OpinionTypeDTO> returnAppOpinions = loanChecking.getReturnAppOpinions();
		/*if (returnAppOpinions != null && returnAppOpinions.size() > 0) {
			for (OpinionTypeDTO returnAppOpinion : returnAppOpinions) {
				initOpinion(loanApplyCase, returnAppOpinion.getOpinionTypeCode(),
						returnAppOpinionContent,caseResult);
			}
		}*/
		// 拒绝代码
//		List<OpinionTypeDTO> checkOpinionTypes = loanChecking.getCheckOpinionTypes();
		/*if (checkOpinionTypes != null && checkOpinionTypes.size() > 0) {
			for (OpinionTypeDTO checkOpinionType : checkOpinionTypes) {
				initOpinion(loanApplyCase, checkOpinionType.getOpinionTypeCode(),opinionContent,caseResult);
			}
		}*/
	}
	private void initOpinion( LoanApplyCaseDTO loanApplyCase , String opinionTypeCode ,String remark ,String caseResult ) throws MarvelsBusinessException {
		OpinionDTO opinion = new OpinionDTO();
		opinion.setLoanId(loanApplyCase.getLoanId());
		opinion.setCaseResult(caseResult);
		opinion.setCaseVersion(loanApplyCase.getCaseVersion());//在调用activiti之前调用记录
		UserDTO user = CacheUtils.getCacheUser();
		opinion.setCreatedBy(user == null ? "system" : user.getUserCode());
		opinion.setNodeCode(loanApplyCase.getCurNodeCode());
		opinion.setNodeName(loanApplyCase.getCurNodeName());
		opinion.setOpinion(remark);
		//String opinionTypeCode,String nodeCode,String patternCod
		//简版新核心以下功能不需要
		/*OpinionTypeDTO opinionTypeDTO = this.opinionTypeService.findOpinionType(opinionTypeCode,loanApplyCase.getCurNodeCode(),loanApplyCase.getPatternCode());
		opinion.setOpinionClass(opinionTypeDTO.getOpinionClass());
		opinion.setOpinionTypeApp(opinionTypeDTO.getOpinionTypeApp());
		opinion.setOpinionTypeCode(opinionTypeDTO.getOpinionTypeCode());
		opinion.setOpinionTypeName(opinionTypeDTO.getOpinionTypeName());*/
		opinion.setUpdatedBy(user == null ? "system" : user.getUserCode());
		saveOpinion(opinion);
	}
	
	@Override
	public List<OpinionDTO> selectOpinionsByCaseVersion(LoanApplyCaseDTO loanApplyCase) throws MarvelsBusinessException {
		OpinionDTO params = new OpinionDTO();
		params.setLoanId(loanApplyCase.getLoanId());
		params.setCaseVersion(loanApplyCase.getCaseVersion());
		params.setNodeCode(loanApplyCase.getCurNodeCode());
		params.setUpdatedBy(params.obtainLoginUserName());
		List<OpinionDTO> list = opinionDao.selectOpinionsByCaseVersion(params);
		return list;
	}
	
	@Override
	public List<OpinionDTO> selectApproveResultLogByLoanId(LoanApplyCaseDTO loanApplyCase) throws MarvelsBusinessException {
		List<OpinionDTO> logs = opinionDao.selectApproveResultLogByLoanId(loanApplyCase);
		return logs;
	}

}
